//
//  TestUnderObject.m
//  LearnObjectiveC
//
//  Created by client on 2017/9/27.
//  Copyright © 2017年 吴可高. All rights reserved.
//
#include <stdio.h>
#include <stdarg.h>

#import "TestUnderObject.h"

#if !defined(OptFunc)
#define OptFunc(arg0,arg1)({\
    NSLog(@"arg1:%d,arg2:%d",arg0,arg1);\
    (arg0)+(arg1);\
})
#endif

#if !defined(minimum)
#define minimum(a,b)({\
    (a) < (b) ? (a):(b);\
})
#endif

#if !defined(maximum)
#define maximum(a,b)({\
   (a) >(b) ? (a):(b);\
})
#endif

#if !defined(plusValue)
#define plusValue(a,b)({\
    (a) + (b);\
})
#endif

#if !defined(RUNLOOP)
#define RUNLOOP(TO,FROM,CONTENT) do {\
for (int idx=TO; idx <=FROM; idx++)\
    {\
        CONTENT;\
    }\
}while(0)
#endif

#define calllinked(NAME)wyd##NAME()

void wydlinked(){\
    printf("name is %s\n","wyd");\
}

void wydCClass(){\
    printf("name is %s\n","CClass");
}

#if !defined(c_debug)
#define c_debug(format, ...) do {\
    fprintf (stderr, format, ##__VA_ARGS__);\
    printf("\n");\
    printf(format,##__VA_ARGS__);\
}while(0)
#endif

#if !defined(OC_DEBUG)
#define OC_DEBUG(format,...) do{\
    NSLog(format,##__VA_ARGS__);\
}while(0)
#endif

#if !defined(swap)
#define swap(x,y) do {\
    x = x +y;\
    y = x -y;\
    x = x -y;\
}while(0)
#endif


@implementation TestUnderObject
- (instancetype)init
{
    self = [super init];
    if (self) {
        int a= 10;
        int b =100;
        swap(a, b);
        NSLog(@"a=%zd",a);
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        unsigned int count = 0;
        objc_property_t * propertys = class_copyPropertyList([self class], &count);
        for (int i =0; i < count; i++) {
            objc_property_t property = propertys[i];
            const char * propertyName = property_getName(property);
            NSString * key = [NSString stringWithUTF8String:propertyName];
            id  value=  [coder decodeObjectForKey:key];
            [self setValue:value forKey:key];
        }
    }
    return self;
}

-(void)encodeWithCoder:(NSCoder *)aCoder
{
    [super encodeWithCoder:aCoder];
    unsigned int count = 0;
    objc_property_t * propertys = class_copyPropertyList([self class], &count);
    for (int i = 0; i < count; i++) {
        objc_property_t property = propertys[i];
        const char * propertyName = property_getName(property);
        NSString * key = [NSString stringWithUTF8String:propertyName];
        NSString * value = [self valueForKey:key];
        [aCoder encodeObject:value forKey:key];
    }
}

@end
